
<script id="address_list" type="text/html">
    	<@each list as value i@>	        
	        <div class="weui-row">
		        <label class="weui_cell weui_check_label weui-col-80" for="s<@value.address_id@>">			        	
		          <div class="weui_cell_hd weui-col-20">
		          		<input type="hidden" id="city_id_<@value.address_id@>" value="<@value.city_id@>">
			            <input type="radio" class="weui_check" aid='<@value.address_id@>' name="choice_address" id="s<@value.address_id@>">
			            <i class="weui_icon_checked"></i>
		          </div>
		          <div class="weui_cell_bd weui_cell_primary weui-col-80">
			            <div>
					        <span id="name<@value.address_id@>"><@value.name@></span><span id="tel<@value.address_id@>"><@value.telephone@></span>
					    </div>
					    <div>
					    	<span id="address<@value.address_id@>"><@value.address@></span>
						</div>
		          </div>				          			
		        </label>
		        <div class="weui-col-20">
		          	<a href="javascript:;" aid='<@value.address_id@>' class="edit_address"></a>
		        </div>
	        </div>
        <@/each@>
</script>

<div id="choice" class='weui-popup-container popup-bottom'>
      <div class="weui-popup-overlay"></div>
      <div class="weui-popup-modal">
        <div class="toolbar">
          <div class="toolbar-inner">
            <a href="javascript:;" class="picker-button close-popup">关闭</a>
            <h1 class="title">选择收货地址</h1>
          </div>
        </div>
        <div class="modal-content">
            <div id="address">
          		<div class="weui_cells weui_cells_checkbox" id='mobile_address_list'>
          			
		        </div>          
          </div>
		  <div id="add_address" class="weui-row">
		  	<div class="weui-col-20">
		  		<a class="add_address"></a>
		  	</div>
		  	<div class="weui-col-80 weui_panel_access"><a class="weui_panel_ft" href="javascript:void(0);">添加新地址</a></div>
		  </div>	
        </div>
      </div>
    </div>

<div id="eidt_address_form" class='weui-popup-container popup-bottom'>
<div class="weui-popup-overlay"></div>
      <div class="weui-popup-modal">
        <div class="toolbar">
          <div class="toolbar-inner">
            <a href="javascript:;" class="picker-button close-popup">关闭</a>
            <h1 class="title">收货地址</h1>
          </div>
        </div>
        
        <input type="hidden" id="update_type" value="add"> 
        <input type="hidden" id="update_address_id" value="0">      
          
        <div class="modal-content">
        	<div class="weui_cells weui_cells_form">
        		<div class="weui_cell">
			        <div class="weui_cell_hd"><label class="weui_label">收货人</label></div>
			        <div class="weui_cell_bd weui_cell_primary">
			          <input class="weui_input" name="name" type="text" placeholder="请输入收货人">
			        </div>
			    </div>
			    <div class="weui_cell">
			        <div class="weui_cell_hd"><label class="weui_label">手机号码</label></div>
			        <div class="weui_cell_bd weui_cell_primary">
			          <input class="weui_input" name="tel" type="text" placeholder="请输入手机号码">
			        </div>
			    </div>
			    <div class="weui_cell">
			        <div class="weui_cell_hd"><label class="weui_label">地区信息</label></div>
			        <div class="weui_cell_bd weui_cell_primary">
			          <input class="weui_input" name="area" id="area" type="text" placeholder="请输入地区信息">
			        </div>
			    </div>
			    <div class="weui_cell">
			        <div class="weui_cell_hd"><label class="weui_label">详细地址</label></div>
			        <div class="weui_cell_bd weui_cell_primary">
			           <input class="weui_input" name="address" type="text" placeholder="请输入详细地址"> 			          
			        </div>
			    </div>
			    <div class="weui_btn_area">
			        <a class="weui_btn weui_btn_primary" href="javascript:" id="add_address_button">确定</a>
			      </div>
        	</div>
        </div>
      </div>	
</div>    
    
<script>

//清空编辑地址的表单
function clear_form(){
	$('input[name="name"]').val('');
	$('input[name="tel"]').val('');
	$('input[name="area"]').val('');
	$('input[name="address"]').val('');
}

//计算,更新运费
function update_transport(address_id,city_id,weight){
	
	$.post(
		'{:url("cart/update_transport")}',
		{	address_id:address_id,	
			city_id:city_id,			
			weight:weight
		},
		function(d){
			if(d.error){
				$.toast(d.error, "forbidden");
			}else{
				$('#city_id').val(city_id);				
				 $('#wrp-btn').remove();
			
					$('#order_yunfei').text(d['transport']['price']);				
					//t=(parseFloat($('#order_amount_sig').text())+parseFloat(d['transport']['price'])).toFixed(2); 				
					//$('#order_amount').text(t);
				
			}
		}
	);
	
}

$(function(){
	
/**
 * 选择收货地址
 */
$('.weui_check').live('click', function() {
	
	var address_id=$(this).attr('aid');
	
	var name=$('#name'+address_id).text();
	var tel=$('#tel'+address_id).text();
	var address=$('#address'+address_id).text();
	var city_id=$('#city_id_'+address_id).val();
	
	$('#express-name').html(name);
    $('#express-person-phone').html(tel);
    $('#express-address').html(address);
    
    $('#city_id').val(city_id);
    $('#address_id').val(address_id);
    
    $.showLoading();
    
    update_transport(address_id,city_id,$('#weight').val());
    
    setTimeout(function (){
        $.hideLoading();
        $.closePopup(); 
    }, 500);

});
/**
 * 编辑收货地址
 */
$('.edit_address').live('click', function() {
	$.closePopup()
	clear_form();
	$.showLoading();
	var aid=$(this).attr('aid');
	$.get(
		'{:url("cart/get_address")}',
		{
			aid:aid
		},
		function(d){
			if(d.error){
				$.hideLoading();
				$.toast(d.error, "forbidden");
			}else{				
					$('#update_type').val('edit');
					$('#update_address_id').val(aid);
					
					$('input[name="name"]').val(d['address']['name']);
					$('input[name="tel"]').val(d['address']['telephone']);
					$('input[name="area"]').val(d['area']);
					$('input[name="address"]').val(d['address']['address']);
			}
		}
	);
	   
	setTimeout(function (){
        $.hideLoading();
        $("#eidt_address_form").popup();
    }, 500);   

});
/**
 * 新增收货地址
 */
$('#add_address').live('click', function() {
	$.closePopup(); 
	clear_form();
	
	$('#update_type').val('add');
	$('#update_address_id').val('0');
	
	$("#eidt_address_form").popup();
});
$("#area").cityPicker({
        title: "选择地区"
});
//添加收货地址
$('#add_address_button').live('click', function() {
	
	var name=$('input[name="name"]').val();
	var tel=$('input[name="tel"]').val();
	var area=$('input[name="area"]').val();
	var address=$('input[name="address"]').val();
	
	if(name==''){
		$.toast('收货人必填',"forbidden");
	}
	if(tel==''){
		$.toast('联系电话必填',"forbidden");
	}
	if(area==''){
		$.toast('地区信息必填',"forbidden");
	}
	if(address==''){
		$.toast('详细地址必填',"forbidden");
	}


	var strs= new Array();
	strs=area.split(" "); //字符分割 
	
	$.showLoading();
	
	$.post(
		'{:url("cart/update_address")}',
		{	
			name:name,
			tel:tel,			
			address:address,
			province:strs[0],
			city_id:strs[1],
			country_id:strs[2],
			type:$('#update_type').val(),
			address_id:$('#update_address_id').val(),
			weight:$('#weight').val(),
			
		},
		function(d){
			if(d.error){
				$.toast(d.error, "forbidden");
			}else{
				
				$('#city_id').val(d.city_id);
				$('#address_id').val(d.address_id);
				$('#express-name').html(name);
			    $('#express-person-phone').html(tel);
			    $('#express-address').html(address);
				$('#wrp-btn').remove();
				
				
				$('#order_yunfei').text(d['transport']['price']);				
				t=parseFloat($('#order_amount_sig').text())+parseFloat(d['transport']['price']); 				
				//$('#order_amount').text(t.toFixed(2));
				
			}
		}
	);

	setTimeout(function (){
        $.hideLoading();
        $.closePopup(); 
    }, 500);
	
});	
//加载收货地址
$('#express_address').click(function(){
	
	$.showLoading();
	$("#mobile_address_list").html('');
		$.get(
			'{:url("cart/get_address_list")}',
			function(d){
				if(d.list){
					var data ={
						list:d.list
					};

					var html = template('address_list', data);

					$("#mobile_address_list").append(html);

				}
			}

		);

		setTimeout(function (){
			$.hideLoading();
			$("#choice").popup();
		}, 500);
	
	});


});	
</script>    
    
